package com.akaovi.chatroom.scene;

import com.akaovi.chatroom.client.Client;
import com.akaovi.chatroom.process.ChatMessManage;
import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.text.Font;

/**
 * @author akaovi
 * @version 1.0
 * @description  会话板
 * @date 2022/12/9 18:25
 * @since 2022/12/9 18:25
 */
public class SendMessBoard {
    static Label mainTitle = new Label();
    public static TextArea show = new TextArea();
    static TextArea toBeSend = new TextArea();
    static Button sendBtn = new Button("发送");
    static String currentId = "";

    static {
        mainTitle.setFont(new Font(20));
        MainScene.gridPane.add(mainTitle, 3, 0, 7, 1);
        MainScene.gridPane.add(show, 3, 1, 7, 6);
        show.setEditable(false);
        show.setFocusTraversable(false);
        MainScene.gridPane.add(toBeSend, 3, 7, 7, 3);
        toBeSend.setFocusTraversable(false);
        MainScene.gridPane.add(sendBtn, 8, 9, 2, 1);
        sendBtn.setMinWidth(70);
        sendBtn.setMinHeight(30);
        sendBtn.setFocusTraversable(false);

        // 初始不可见
        mainTitle.setVisible(false);
        toBeSend.setVisible(false);
        show.setVisible(false);
        sendBtn.setVisible(false);

        sendBtn.setOnAction(e -> {
            Platform.runLater(() -> {
                String text = toBeSend.getText();
                if (text == null || text.length() == 0) {
                    //todo 调试
                    return;
                }
                Client.ot.sendMess(text, Integer.parseInt(currentId));
                ChatMessManage.add("我：" + text);
                show.setText(ChatMessManage.get());
                toBeSend.setText("");
            });
        });
    }

    /**
     * 展示会话板
     */
    public static void showSendMessBoard() {
        if (!mainTitle.isVisible() && mainTitle.getText().length() != 0) {
            mainTitle.setVisible(true);
            toBeSend.setVisible(true);
            show.setVisible(true);
            sendBtn.setVisible(true);
            MainScene.coverCommonIcon();
        } else {
            MainScene.showCommonIcon();
        }
    }

    /**
     * 不展示
     */
    public static void coverSendMessBoard() {
        if (mainTitle.isVisible()) {
            mainTitle.setVisible(false);
            toBeSend.setVisible(false);
            show.setVisible(false);
            sendBtn.setVisible(false);
        }
    }

    /**
     * 更改当前对话的用户账号
     * @param account 账号
     */
    public static void changeCurrentUser(String account) {
        SendMessBoard.currentId = account;
    }


    /**
     * 重置
     */
    public static void restart() {
        mainTitle.setText("");
        show.setText("");
        toBeSend.setText("");
        currentId = "";
        mainTitle.setVisible(false);
        toBeSend.setVisible(false);
        show.setVisible(false);
        sendBtn.setVisible(false);
    }

    /**
     * 响应消息更新
     */
    public static void callForText() {
        show.setText(ChatMessManage.get());
    }
}
